package com.ITArmy.service.impl;

import com.ITArmy.beans.Blog;
import com.ITArmy.beans.Collections;
import com.ITArmy.beans.Serialize;
import com.ITArmy.beans.Shows.ReadBlog;
import com.ITArmy.beans.Shows.UserCollection;
import com.ITArmy.beans.User;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.dao.BlogMapper;
import com.ITArmy.dao.CollectionsMapper;
import com.ITArmy.dao.SerializeMapper;
import com.ITArmy.dao.UserMapper;
import com.ITArmy.service.CollectionService;
import com.ITArmy.util.CheckMessage;
import com.ITArmy.util.GetUserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService {
    GetUserSession getUserSession = new GetUserSession();
    @Autowired
    CollectionsMapper collectionsMapper;
    @Autowired
    BlogMapper blogMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    SerializeMapper serializeMapper;

    //用户收藏博客
    @Override
    public ServerResponse collectionBlog(HttpServletRequest req, long blogId) {
        User user = getUserSession.getUser(req);
        Collections collections = new Collections();
        collections.setuId(user.getuId());
        collections.setbId(blogId);
        collections.setCoTime(new Date());
        int resultCount = collectionsMapper.insertSelective(collections);
        CheckMessage check=new CheckMessage();
        return check.getMessage(resultCount,"收藏成功","收藏失败");
    }

    //用户收藏连载
    @Override
    public ServerResponse collectionSerialize(HttpServletRequest req, long serializeId) {
        User user = getUserSession.getUser(req);
        Collections collections = new Collections();
        collections.setuId(user.getuId());
        collections.setsId(serializeId);
        int resultCount = collectionsMapper.insertSelective(collections);
        CheckMessage check=new CheckMessage();
        return check.getMessage(resultCount,"收藏连载成功","收藏连载失败");
    }

    //根据用户id显示用户所有收藏的博客
    @Override
    public ServerResponse getBlogCollection(HttpServletRequest request) {
        List<Collections> collections = collectionsMapper.selectAllCollectionChapterId(getUserSession.getUser(request).getuId());
        if (collections.size() == 0) {
            return ServerResponse.createByErrorMesage("您还没有任何的收藏信息哦...");
        }
        List<UserCollection> blogs = new ArrayList<>();
        for (int i = 0; i < collections.size(); i++) {
            User user = userMapper.selectByPrimaryKey(collections.get(i).getuId());
            Blog blog = blogMapper.selectByPrimaryKey(collections.get(i).getbId());
            blogs.add(new UserCollection(user, collections.get(i), blog));
        }
        return ServerResponse.createBySuccess("搜索成功...", blogs);
    }

    //根据用户的id显示所有的连载的收藏
    @Override
    public ServerResponse getSerializeCollection(HttpServletRequest request) {
        List<Collections> collections = collectionsMapper.selectAllCollectionChapterId(getUserSession.getUser(request).getuId());
        if (collections.size() == 0) {
            return ServerResponse.createByErrorMesage("您还没有任何的收藏信息...");
        }
        List<Serialize> serializes = new ArrayList<Serialize>();
        for (int i = 0; i < collections.size(); i++) {
            serializes.add(serializeMapper.selectByPrimaryKey(collections.get(i).getsId()));
        }
        return ServerResponse.createBySuccess("搜索成功...", serializes);
    }

    //用户删除收藏的博客
    @Override
    public ServerResponse deleteBlogCollection(HttpServletRequest request, long blogId) {
        User user = getUserSession.getUser(request);
        int resultCount = collectionsMapper.deleteCollectionBlog(user.getuId(), blogId);
        CheckMessage check=new CheckMessage();
        return check.getMessage(resultCount,"删除成功","删除失败");
    }

    //用户删除收藏得连载
    @Override
    public ServerResponse deleteSerializeCollection(HttpServletRequest request, long SerializeId) {
        User user = getUserSession.getUser(request);
        int resultCount = collectionsMapper.deleteCollectionSerilaize(user.getuId(), SerializeId);
        CheckMessage check=new CheckMessage();
        return check.getMessage(resultCount,"删除成功","删除失败");
    }
}
